/*
* @file_name: thread_pool-inl.h
* @date: 2021/08/30
* @author: oaho
* Copyright @ hz oaho, All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef OAHO_TASK_EXECUTOR_POOL_INL_H
#define OAHO_TASK_EXECUTOR_POOL_INL_H
#include <limits>
#include "thread_pool.h"
template<typename Container>
round_robin::round_robin(Container& c){
	_current_ = c.begin()->first;
}
template<typename Container, typename F>
inline void round_robin::put(Container& c, F&& f)
{
	auto task_executor = get(c);
	task_executor->async(std::move(f));
	return task_executor;
}

template<typename Container>
inline typename Container::value_type::second_type round_robin::get(Container& c)
{
	auto iter = c.lower_bound(_current_);
	++iter;
	if (iter == c.end())iter = c.begin();
	auto task_executor = iter->second;
	_current_ = iter->first;
	return task_executor;
}

template<typename TaskExecutor>
thread_pool<TaskExecutor>::thread_pool(size_t thread_num):thread_num(thread_num), running(false)
{
	init();
}

template<typename TaskExecutor>
void thread_pool<TaskExecutor>::init()
{
	for (int i = 0; i < thread_num; ++i){
		std::shared_ptr<TaskExecutor> ptr(new TaskExecutor());
		ptr->start();
		std::thread::id id = ptr->get_thread_id();
		_pool_.insert(std::make_pair(std::move(id), std::move(ptr)));
	}
}

template<typename TaskExecutor>
thread_pool<TaskExecutor>::~thread_pool(){
	stop();
}
template<typename TaskExecutor>
inline void thread_pool<TaskExecutor>::start()
{
	if (!running){
		std::for_each(_pool_.begin(), _pool_.end(), [](const std::pair<std::thread::id, std::shared_ptr<TaskExecutor>>& _t_) {
			_t_.second->start();
		});
		running = true;
	}
}
template<typename TaskExecutor>
inline void thread_pool<TaskExecutor>::stop()
{
	if (running){
		std::for_each(_pool_.begin(), _pool_.end(), [](const std::pair<std::thread::id, std::shared_ptr<TaskExecutor>>& _t_) {
			_t_.second->stop();
		});
		running = false;
	}
}
template<typename TaskExecutor>
template<typename policy>
inline TaskExecutor& thread_pool<TaskExecutor>::get_poller(bool fetch_current_thread) {
	static policy p(_pool_);
	using iterator = typename container::iterator;
	iterator iter = _pool_.end();
	if(fetch_current_thread){
		std::thread::id current_id = get_current_thread_id();
		iter = _pool_.find(current_id);
	}
	return iter == _pool_.end() ? *(p.get(_pool_)): *(iter->second);
}

template<typename TaskExecutor>
	template<typename F, typename...Args> 
inline void thread_pool<TaskExecutor>::async(F&& f, Args&&...args){
	
}
template<typename TaskExecutor>
inline const std::thread::id thread_pool<TaskExecutor>::get_current_thread_id(){
	return std::this_thread::get_id();
}

#endif // !OAHO_TASK_EXECUTOR_POOL_INL_H
